﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;
using StockFinder.Model.Constants;
using Db4objects.Db4o.Config;

namespace StockFinder.Model
{
    /// <summary>
    /// Container 
    /// </summary>
    public class PriceHistoryContainer : IDisposable    
    {
        private List<Symbol> _Symbols = new List<Symbol>();
        public List<Symbol> Symbols
        {
            get
            {
                return _Symbols;
            }
        }

        /// <summary>
        /// Gets/sets the last price date
        /// </summary>
        public DateTime LastPriceDate { get; set; }

        private static volatile PriceHistoryContainer _Instance;
        private static object _SyncRoot = new object();
        private static IObjectContainer database;

        public PriceHistoryContainer() 
        { 
            
        }

        //public static PriceHistoryContainer Instance
        //{
        //    /* TODO: introduce a mutex so only one thread can have access at a time */

        //    get
        //    {
        //        if (_Instance == null)
        //        {
        //            lock (_SyncRoot)
        //            {
        //                if (_Instance == null)
        //                {
        //                    IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
        //                    config.Common.ObjectClass(typeof(PriceHistoryContainer)).CascadeOnUpdate(true);
        //                    config.Common.ObjectClass(typeof(Symbol)).CascadeOnUpdate(true);

        //                    database = Db4oEmbedded.OpenFile(config, 
        //                        ApplicationConfiguation.ObjectDatabaseSRootDirectory + ObjectDatabaseFilenames.PRICE_HISTORY);

        //                    //2 a. get or create PriceHistoryContainer
        //                    List<PriceHistoryContainer> priceHistoryContainers = database.Query<PriceHistoryContainer>().ToList();                          

        //                    //PriceHistoryContainer doesnt exist so create it
        //                    if (priceHistoryContainers == null || priceHistoryContainers.Count() == 0)
        //                    {
        //                        _Instance = new PriceHistoryContainer();
        //                        database.Store(_Instance);
        //                    }
        //                    else
        //                    {
        //                        //should only be 1 price history container
        //                        _Instance = priceHistoryContainers[0];
        //                    }
        //                }
        //            }
        //        }

        //        return _Instance;
        //    }
        //}

        //public static void Save()
        //{
        //    database.Store(Instance);
        //}

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // get rid of managed resources
                if (_Instance != null)
                {
                    if (database != null)
                    {
                        database.Dispose();
                    }
                }
            }
            // get rid of unmanaged resources
        }
    }
}
